package com.spring.service;

import com.spring.dao.IBaseDao;
import com.spring.dao.IGroupDao;
import com.spring.exception.UserException;
import com.spring.model.User;
import javax.annotation.Resource;
import java.util.List;

/**
 * Created by hbl on 2017/12/27.
 */
public class UserServiceImp extends BaseServiceImp<User> implements IUserService {

    @Resource(name = "groupHibernateDao")
    private IGroupDao groupHibernateDao;

    @Override
    @Resource(name="userHibernateDao")
    public void setBaseDao(IBaseDao<User> baseDao) {
        super.setBaseDao(baseDao);
    }

    @Override
    public void add(User u, int gid) {
        User.Group g = groupHibernateDao.load(gid);
        if (g == null)
           throw new UserException("添加的用户组不存在");
        u.setGroup(g);
        this.getBaseDao().add(u);
    }

    @Override
    public void addA(User u) {
      this.getBaseDao().add(u);
    }

    @Override
    public List<User> listByGroup(int gid) {
        String hql = "from User where group.id=?";
        return this.getBaseDao().list(hql, gid);
    }
}
